home *** CD-ROM | disk | FTP | other *** search
RISC OS BBC BASIC V Source | 1994-02-05 | 4.7 KB | 197 lines |
- >HeapTest
- _heap_init :
- Initialise memory heap
- Globals for test program
- quit% =
- a% = 0
- b% = 0
- blk% = 0
- amount$ = ""
- g = 0
- A simple menu to test the heap procedures
- '"Slot size currently ";~_SlotSize%
- "Select from..."
- "1= fetch"'"2= return"'"3= resize"'"4= quit"
- g =
- - 48
-
- "size to fetch: &"a$
-
- a$ <> ""
- a% =
- ("&" + a$)
- ! blk% =
- _heap_get(a%)
-
- blk% > 0
- 0
- '"Block allocated at &" +
- ~blk%
-
- 0
- '"Unable to claim enough memory"
-
-
- "
- "block to return: &"a$
-
- a$ <> ""
- a% =
- ("&" + a$)
- b% = a%
-
- _heap_release(a%)
-
- (a% = 0)
- &;
- '"Block at &" +
- ~b% + " has been released"
-
- (,
- '"No block exists at &"+
-
-
- ,"
- "Block to resize: &"a$
- -0
- "Amount (-ve to decrease): &"amount$
-
- a$ <> ""
- a% =
- ("&" + a$)
- b% = a%
- 1(
- _heap_resize(a%,
- (amount$))
-
- (a% > 0)
- 3<
- '"Block at &" +
- ~b% + " has been resized ";
- 45
- a% <> b%
- "(now at &"+
- ~a%+")"
-
- 68
- a% = -3
- '"No block exists at &"+
- 7<
- a% = -2
- '"Unable to claim enough memory"
- 8>
- a% = -1
- '"Block size is now 0 or negative"
-
-
- quit% =
- quit%
- -- Heap Procedures ------------------------------------------------
- Global variables used:
- _Heap% Start of heap
- _SlotSize% Size of current WimpSlot
- _PageSize% Size of a memory page
- _HeapEnd% End of heap
- _heap_init
- JA_Heap% =
- heap starts just after end of allocated memory
- "OS_ReadMemMapInfo"
- _PageSize%
- enlarge wimpslot (1 page) and create private heap in new memory
- _SlotSize% = _Heap% - &8000
- "Wimp_SlotSize",_SlotSize% + _PageSize%,-1
- _SlotSize%
- O"_HeapEnd% = _SlotSize% + &8000
- _HeapEnd% <= _Heap%
- 0,"Can't claim space for heap"
- "OS_Heap",0,_Heap%,,_HeapEnd% - _Heap%
- _heap_get(size%)
- maxfree%,nrpages%,oldheapend%,ptr%
- Returns pointer to new memory block
- -1 if claim fails due to lack of memory
- "OS_Heap",1,_Heap%
- ,,maxfree%
- size% > maxfree%
- largest free block is too small - try to enlarge wimpslot and heap
- [H nrpages% = 1 + (size%
- _PageSize%) :
- required # pages of memory
- "Wimp_SlotSize",_SlotSize% + nrpages% * _PageSize%,-1
- _SlotSize%
- oldheapend% = _HeapEnd%
- ^& _HeapEnd% = _SlotSize% + &8000
- "OS_Heap",5,_Heap%,,_HeapEnd% - oldheapend%
- "OS_Heap",1,_Heap%
- ,,maxfree% :
- do we have enough now?
- size% > maxfree%
- ptr% = -1
- "OS_Heap",2,_Heap%,,size%
- ,,ptr%
- = ptr%
- _heap_release(
- ptr%)
- maxfree%,nrpages%,flg%
- Returns 0 if block released OK
- Returns -1 if operation failed (i.e. block doesn't exist)
- "XOS_Heap",3,_Heap%,ptr%
- ;flg% :
- Free the block
- (flg%
- 1) = 0
- Block was released successfully...
- "OS_Heap",1,_Heap%
- ,,maxfree% :
- Get info on heap
- maxfree% > _PageSize%
- rC
- more than 1 page free - try to shrink heap (page by page)
- s0 nrpages% = 0 :
- # pages of memory freed
- t
- u5
- "XOS_Heap",5,_Heap%,,-_PageSize%
- ;flg%
-
- (flg%
- 1) = 0
- nrpages% += 1
-
- (flg%
- 1) <> 0
- nrpages% > 0
- {6
- successfully shrunk heap - shrink WimpSlot
- |O
- "Wimp_SlotSize",_SlotSize% - nrpages% * _PageSize%,-1
- _SlotSize%
- }( _HeapEnd% = _SlotSize% + &8000
- ~
- ?
- "OS_Heap",5,_Heap%,,(_HeapEnd% - _Heap%) - _Heap%!12
- Return 0 to signal successful release of block
- ptr% = 0
- Error occured trying to free the block, return -1 to signal to the
- program that something went wrong (normally the program would ignore
- this anyway)
- ptr% = -1
- _heap_resize(
- ptr%,change%)
- flg%
- Returns a new pointer to the block (it may be moved in memory). Any data
- in the block will be copied to the new location if necessary.
- Returns -1 if the block now has a size of 0 or less
- Returns -2 if claim fails due to lack of memory
- Returns -3 if block does not exist
- "XOS_Heap",6,_Heap%,ptr%
- ;flg% :
- Read size of block to check it exists
- (flg%
- It doesn't exist..
- ptr% = -3
- It does, so attempt to perform resize..
- "XOS_Heap",4,_Heap%,ptr%,change%
- ,,ptr%;flg%
- (flg%
- ptr% = -2
-